package com.itheima.reggie.controller;

import com.itheima.reggie.commons.R;

import com.itheima.reggie.dto.SetmealDto;

import com.itheima.reggie.pojo.Page;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@Api(tags ="套餐类")
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetMealController extends BaseController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private DishService dishService;

    @PostMapping
    public R<String> saveMealDish(@RequestBody SetmealDto setmealDto) {
        log.info("添加套餐");
        setMealService.saveWithDish(setmealDto);
        return R.success("保存菜单成功");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> findPageByName(@RequestParam("page") Integer pageNum, Integer pageSize, String name) {
        Page<SetmealDto> page = setMealService.findByPage(pageNum, pageSize, name);
        return R.success(page);
    }

    @DeleteMapping
    public R<String> deleteByIds(@RequestParam Long[] ids) {
        setMealService.deleteById(ids);
        return R.success("删除成功");
    }

    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        setMealService.updateStatus(status, ids);
        return R.success("修改成功");
    }

    @GetMapping("list")
    public R<List<Setmeal>> findDishById(Long categoryId, Integer status) {
        List<Setmeal> dishList = setMealService.findBysetmealId(categoryId, status);
        return R.success(dishList);
    }

    @GetMapping("/{setmealId}")
    public R<SetmealDto> findById(@PathVariable("setmealId") Long setmealId) {
        SetmealDto setmeal = setMealService.findSetmeal(setmealId);
        return R.success(setmeal);
    }

    @GetMapping("/dish/{setmealId}")
    public R<List<Map<String, Object>>> findSetmealDish(@PathVariable Long setmealId) {
        List<Map<String, Object>> setmealDishes = setMealService.findSetmealDish(setmealId);
        return R.success(setmealDishes);
    }

    @PutMapping
    public R<String> updateSetMeal(@RequestBody SetmealDto setmealDto) {
        setMealService.updateSetMeal(setmealDto);
        return R.success("修改成功");
    }
}
